/*
 * @Author: yuki
 * @Date: 2025-11-04 21:25:17
 * @LastEditTime: 2025-11-06 14:53:15
 * @Description: redux配置文件
 */
import { configureStore } from '@reduxjs/toolkit';
import type { TypedUseSelectorHook } from 'react-redux';
import { useSelector, useDispatch, shallowEqual } from 'react-redux';

import userReducer from './module/login';

const store = configureStore({
  reducer: {
    user: userReducer
  }
});

type RootStateType = ReturnType<typeof store.getState>;
type DispatchType = typeof store.dispatch;

export const useAppSelector: TypedUseSelectorHook<RootStateType> = useSelector;
export const shallowEqualApp = shallowEqual;
export const useAppDispatch: () => DispatchType = useDispatch;

// 自定义hooks，用于获取某个模块状态
export function useModuleSelector<K extends keyof RootStateType, TSelected>(
  moduleName: K,
  selector: (state: RootStateType[K]) => TSelected,
  equalityFn?: (left: TSelected, right: TSelected) => boolean
): TSelected {
  return useAppSelector(
    (state: RootStateType) => selector(state[moduleName]),
    equalityFn
  );
}

// 导出登录模块的action creators
export * from './module/login';

export default store;
